// ButtonSkinLoader.cpp: implementation of the CButtonSkinLoader class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ButtonSkinLoader.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CButtonSkinLoader::CButtonSkinLoader()
:m_hOwnerWnd(NULL)
{

}

CButtonSkinLoader::~CButtonSkinLoader()
{

}

void CButtonSkinLoader::Init(HWND hWnd)
{
	m_hOwnerWnd = hWnd;
}

void CButtonSkinLoader::ClearSkin()
{
	if(m_dcNormal.GetSafeHdc())
		m_dcNormal.DeleteDC();
	if(m_dcSel.GetSafeHdc())
		m_dcSel.DeleteDC();
	if(m_dcHover.GetSafeHdc())
		m_dcHover.DeleteDC();
	if(m_dcDisable.GetSafeHdc())
		m_dcDisable.DeleteDC();
}

void CButtonSkinLoader::SplitSkin(CDynamicMemDC* pDC)
{
	int nWidth = pDC->Width()/4;
	int nNormalPos = 0;
	int nSelPos = nWidth;
	int nHoverPos = 2*nWidth;
	int nDisablePos = 3*nWidth;

	SplitSkinByPos(pDC, nWidth, nNormalPos, nSelPos, nHoverPos, nDisablePos);
}
void CButtonSkinLoader::SplitSkinByPos(CDynamicMemDC* pDC, int nWidth, int nNormalPos, int nSelPos, int nHoverPos, int nDisablePos)
{
	safe_assert(pDC);

	m_dcNormal.CreateMemDC(nWidth, pDC->Height());
	m_dcSel.CreateMemDC(nWidth, pDC->Height());
	m_dcHover.CreateMemDC(nWidth, pDC->Height());
	m_dcDisable.CreateMemDC(nWidth, pDC->Height());

	m_dcNormal.BitBlt(0, 0, nWidth, pDC->Height(), pDC, nNormalPos, 0, SRCCOPY);
	m_dcSel.BitBlt(0, 0, nWidth, pDC->Height(), pDC, nSelPos, 0, SRCCOPY);
	m_dcHover.BitBlt(0, 0, nWidth, pDC->Height(), pDC, nHoverPos, 0, SRCCOPY);
	m_dcDisable.BitBlt(0, 0, nWidth, pDC->Height(), pDC, nDisablePos, 0, SRCCOPY);
}

BOOL CButtonSkinLoader::LoadSkin(UINT nID)
{
	safe_assert(::IsWindow(m_hOwnerWnd));
	if(!nID)
		return FALSE;

	ClearSkin();

	CDynamicMemDC dcTemp;
	if(!dcTemp.CreateDCFromBmp(nID))
		return FALSE;

	SplitSkin(&dcTemp);
	::InvalidateRect(m_hOwnerWnd, NULL, TRUE);
	return TRUE;
}

BOOL CButtonSkinLoader::LoadSkinByPos(UINT nID, int nWidth, int nNormalPos, int nSelPos, int nHoverPos, int nDisablePos)
{
	safe_assert(::IsWindow(m_hOwnerWnd));
	if(!nID)
		return FALSE;

	ClearSkin();

	CDynamicMemDC dcTemp;
	if(!dcTemp.CreateDCFromBmp(nID))
		return FALSE;

	SplitSkinByPos(&dcTemp, nWidth, nNormalPos, nSelPos, nHoverPos, nDisablePos);
	::InvalidateRect(m_hOwnerWnd, NULL, TRUE);
	return TRUE;
}

BOOL CButtonSkinLoader::LoadMultiSkin(UINT nIDNormal, UINT nIDSel, UINT nIDHover, UINT nIDDisable)
{
	safe_assert(::IsWindow(m_hOwnerWnd));
	if(!nIDNormal || !nIDSel || !nIDHover || !nIDDisable)
		return FALSE;

	ClearSkin();

	BOOL bResult = TRUE;
	bResult |= m_dcNormal.CreateDCFromBmp(nIDNormal);
	bResult |= m_dcSel.CreateDCFromBmp(nIDSel);
	bResult |= m_dcHover.CreateDCFromBmp(nIDHover);
	bResult |= m_dcDisable.CreateDCFromBmp(nIDDisable);
	::InvalidateRect(m_hOwnerWnd, NULL, TRUE);
	return bResult;
}
BOOL CButtonSkinLoader::LoadSkinFile(LPCTSTR lpszBitmap)
{
	safe_assert(::IsWindow(m_hOwnerWnd));
	if(!lpszBitmap)
		return FALSE;

	ClearSkin();

	CDynamicMemDC dcTemp;
	if(!dcTemp.CreateDCFromBmp(lpszBitmap))
		return FALSE;

	SplitSkin(&dcTemp);
	::InvalidateRect(m_hOwnerWnd, NULL, TRUE);
	return TRUE;
}

BOOL CButtonSkinLoader::LoadSkinFileByPos(LPCTSTR lpszBitmap, int nWidth, int nNormalPos, int nSelPos, int nHoverPos, int nDisablePos)
{
	safe_assert(::IsWindow(m_hOwnerWnd));
	if(!lpszBitmap)
		return FALSE;

	ClearSkin();

	CDynamicMemDC dcTemp;
	if(!dcTemp.CreateDCFromBmp(lpszBitmap))
		return FALSE;

	SplitSkinByPos(&dcTemp, nWidth, nNormalPos, nSelPos, nHoverPos, nDisablePos);
	::InvalidateRect(m_hOwnerWnd, NULL, TRUE);
	return TRUE;
}


BOOL CButtonSkinLoader::LoadMultiSkinFile(LPCTSTR lpszBitmapNormal, LPCTSTR lpszBitmapSel, 
					LPCTSTR lpszBitmapHover, LPCTSTR lpszBitmapDisable)
{
	safe_assert(::IsWindow(m_hOwnerWnd));
	if(!lpszBitmapNormal || !lpszBitmapSel || !lpszBitmapHover || !lpszBitmapDisable)
		return FALSE;

	ClearSkin();
	BOOL bResult = TRUE;
	bResult |= m_dcNormal.CreateDCFromBmp(lpszBitmapNormal);
	bResult |= m_dcSel.CreateDCFromBmp(lpszBitmapSel);
	bResult |= m_dcHover.CreateDCFromBmp(lpszBitmapHover);
	bResult |= m_dcDisable.CreateDCFromBmp(lpszBitmapDisable);
	::InvalidateRect(m_hOwnerWnd, NULL, TRUE);
	return bResult;
}

BOOL CButtonSkinLoader::LoadSkinItem(LPCTSTR szUIItemName, LPCTSTR szKeyName)
{
	return LoadSkinFile(g_UIManager.GetSkinFile(szUIItemName, szKeyName));
}
int CButtonSkinLoader::GetSkinWidth()
{
	return m_dcNormal.Width();
}

int	CButtonSkinLoader::GetSkinHeight()
{
	return m_dcNormal.Height();
}
void CButtonSkinLoader::Clear()
{
	ClearSkin();
}


